<!DOCTYPE html>
<html>
<head>
  	<title>ANG.CAST-RECEIVER</title>
  	<script src="//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js"></script>
	<script type="text/javascript" src="../engine/engine-base.js"></script>
  	<script type="text/javascript" src="../engine/engine-receiver.js"></script>
  	<script type="text/javascript" src="../engine/engine-game.js"></script>
	
  	<script>
		//debug flag
		WEB_CHROME_CAST = false;
		WEB_CHROME_PC = true;
		WEB_CHROME_MOBILE = true;
	
    	window.onload = function() {
					
			//Load Engine
			this.loadEngine('canvas-chromecast');
			
			this.loadImage('mario-statics');
			this.loadImage('mario-dinamic-left');
			this.loadImage('mario-dinamic-right');
			var json = loadTileMap('map');
			this.tilemap = new TileMap(json);
			
			this.load = function(){
				this.tilemap.load();
				this.add(this.tilemap);
				
				this.maincharacter = new MainCharacter(100, 100, this.tilemap.subelements[1]);
				
				this.maincharacter.stand_left = new ImageSplitElement('mario-statics', 0, 0, 4, 1, 3, 0);
				this.maincharacter.stand_right = new ImageSplitElement('mario-statics', 0, 0, 4, 1, 2, 0);
				this.maincharacter.walk_left = new SpriteElement('mario-dinamic-left', 0, 0, 3)
				this.maincharacter.walk_right = new SpriteElement('mario-dinamic-right', 0, 0, 3)
				this.maincharacter.up_left = new ImageSplitElement('mario-statics', 0, 0, 4, 1, 1, 0);
				this.maincharacter.up_right= new ImageSplitElement('mario-statics', 0, 0, 4, 1, 0, 0);
				
				this.maincharacter.add(this.maincharacter.stand_left);
				this.maincharacter.add(this.maincharacter.stand_right);
				this.maincharacter.add(this.maincharacter.up_left);
				this.maincharacter.add(this.maincharacter.up_right);
				this.maincharacter.add(this.maincharacter.walk_left);
				this.maincharacter.add(this.maincharacter.walk_right);
				
				this.tilemap.add(this.maincharacter);
			}
			
			//On input 
			this.input = function(){
				if (KEY_NUMS.RIGHT in keysDown) { //RIGHT
					
					if(!tryIntersect(this.maincharacter, 8, 0, this.maincharacter.floor)){
									 
						this.maincharacter.plusX(8);
						
						if((this.canvas.width /2) < this.maincharacter.getX()) this.tilemap.plusX(-8);
					}
					
				}
				if (KEY_NUMS.LEFT in keysDown) { //LEFT
					
					if(!tryIntersect(this.maincharacter, -8, 0, this.maincharacter.floor)){
						
						if(this.maincharacter.getX() > 8) this.maincharacter.plusX(-8);
						
						if((this.canvas.width /2) < this.maincharacter.getX()) this.tilemap.plusX(8);
					}
				}
				if (KEY_NUMS.UP in keysDown) {	 //UP
					
					if(this.maincharacter.gravitylock)
						this.maincharacter.jumpstate = true;
				}
			}
			
			
			this.update = function(){
			
			}
    	}
  	</script>
</head>
	<body style="background-color: black;">
		<canvas id="canvas-chromecast" width="1280" height="720"/>
	</body>
</html>